<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class AdminUserController extends AdminController {
    
    private $_module = 'user';
    
    public function __construct() {
        
        parent::__construct();
    }
    
    public function index( $trash = 0 ) {
        
        $tables = array (
            'id'        =>  'ID',
            'email'     =>  'Email',
            'firstname' =>  'First name',
            'lastname'  =>  'Last name'
        );
        
        if ( $trash ) {
            $tables['restore'] = 'Restore';
        } else {
            $tables['update'] = 'Update';
            $tables['trash'] = 'Trash';
        }
        
        $links =  $trash ? array( 'restore' ) : array( 'update', 'trash' );
        
        $user = new User;
        
        $users = $user->getTrashs( $trash );
        return View::make('admin/common/table', array(
            'items' => $users,
            'fnTitle' => $trash ? 'Trash' : 'All Users',
            'tableTitles'   => array_values($tables),
            'tableKeys'     => array_keys($tables),
            'links'         => $links,
            'module'        => $this->_module
        ));
    }
    
    public function form( $id = '' ) {
        $user = $id ? User::find($id) : '';
        return View::make('admin/user/register', array ( 
            'user'  => $user, 
            'fnTitle' => $id ? Auth::id() == $id ? 'Your Profile' : 'Update User' : 'Add new user' )
        );
    }
    
    public function create() {
        
        $validator = Validator::make(Input::all(), User::$rules);
   
        if ($validator->passes()) {
            $user = new User;
            $userRole = new UserRole;
            
            $user->firstname = Input::get('firstname');
            $user->lastname = Input::get('lastname');
            $user->email = Input::get('email');
            $user->admin = 0;
            $user->active = 1;
            $user->password = Hash::make(Input::get('password'));
            $user->save();
            
            // Save user role
            $userRole->user_id = $user->id;
            $userRole->role_id = Input::get('role');
            $userRole->save();
            
            return Redirect::to('at-admin/user')->with('message', '');
        }
            
        return Redirect::to('at-admin/user/register')->with('message', '')->withErrors($validator)->withInput();
    }
    
    public function update( $id ) {
        
        $rules              = User::$rules;
        $rules['email']     = 'required|email|unique:users,email,' . $id;
        $rules['password']  = 'alpha_num|between:6,12|confirmed';
        $rules['password_confirmation'] = 'alpha_num|between:6,12';
        
        $validator = Validator::make(Input::all(), $rules);
        if ( $validator->passes() ) {
            $user = User::find($id);
            $user->firstname = Input::get('firstname');
            $user->lastname = Input::get('lastname');
            $user->email = Input::get('email');
            $user->admin = 0;
            $user->active = 1;
            
            if ( Input::get('password') ) {
                $user->password = Hash::make(Input::get('password'));
            }
            $user->save();
            return Redirect::to("at-admin/user/register/$id")->with('message', '');
        }
        return Redirect::to("at-admin/user/register/$id")->with('message', '')->withErrors($validator)->withInput();
    }
    
    public function logout() {
        Auth::logout();
        return Redirect::to('at-admin/user/login')->with('message', 'Your are now logged out!');
    }
    
    public function trash($id) {
        
        $user = new User;
        $user->moveToTrash($id);
        return Redirect::to('at-admin/user');
    }
    
    public function restore($id) {
        
        $user = new User;
        $user->moveToTrash($id, 0);
        return Redirect::to('at-admin/user/trashs/1');
    }
}